const App = {
    mixins: [Query],//配置文件，路径/utils/mixins.js
    data() {
        return {
            photo,
            page: {
                value1: '',
                value2: '',
                PageNumber: 1, //页码
                RowAmount: 10,
                Condition: window.btoa(unescape(encodeURIComponent("%%")))
            },
            str1: '',
            str: '',
            form: {

            },
            personal_mapping_tags: [],
            personal_tags: {},
            options: [],
            showDialog1: false
        }
    },
    mounted() {

    },
    methods: {
        check() {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%")));
            this.postgetList(this.page);
        },
        handleDelete(data) {
            this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                let { TableName, primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/queryMultiTableByCondition`,
                    type: "POST",
                    data: {
                        Data: JSON.stringify([{
                            tableName: TableName,
                            Condition: window.btoa(unescape(encodeURIComponent(primary_key + "='" + data[primary_key] + "'"))),
                        }, {
                            tableName: 'personal_mapping_tags',
                            Condition: window.btoa(unescape(encodeURIComponent(primary_key + "='" + data[primary_key] + "'"))),
                        }]),
                    },
                    success: ({ personal_tags, personal_mapping_tags }) => {
                        if (personal_mapping_tags.length > 0 && personal_tags.length == 1) {
                            $.ajax({
                                headers: requestHeaders(),
                                url: `${mh}/generalBusiness/generalQueryByTableName`,
                                type: "POST",
                                data: {
                                    Data: "personal_tags",
                                    Condition: "Personal_Tag_SerialNumber <> '" + data[primary_key] + "' AND Row_Status = '0'",
                                },
                                success: (data) => {
                                    this.form = { Personal_Tag_SerialNumber: '' };
                                    this.personal_mapping_tags = personal_mapping_tags;
                                    this.personal_tags = personal_tags;
                                    this.options = data;
                                    this.showDialog1 = true;
                                },
                                complete: (XMLHttpRequest) => {
                                    requestPretreatment(XMLHttpRequest);
                                },
                                dataType: "json",
                            });
                        } else if (personal_tags.length == 1) {
                            personal_tags[0].Row_Status = '1';
                            let lJSONObject_DataSet = new Object();
                            let lJSONObject_Table = new Object();
                            lJSONObject_Table[TableName] = personal_tags;
                            lJSONObject_DataSet['Modify'] = lJSONObject_Table;
                            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                            lJSONObject_DataSet['Add'] = new Object();
                            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                Operation_Type: '0',
                                Operation_Path: this.relativePath,
                                Operation_Main,
                                Operation_Interface: "generalSaveByRequestBody",
                                Operation_IP: getIpAddress()
                            }]
                            submitSave(lJSONObject_DataSet, (res) => {
                                if (res.Status == 'success') {
                                    this.postgetList(this.page);
                                } else {
                                    this.$message({
                                        type: 'error',
                                        message: '删除失败'
                                    });
                                }
                            })
                            // $.ajax({
                            //     headers: requestHeaders(JSON),
                            //     url: `${mh}/generalBusiness/generalSaveByRequestBody`,
                            //     method: "post",
                            //     data: JSON.stringify({
                            //         Data: lJSONObject_DataSet,
                            //     }),
                            //     success: (res) => {
                            //         if (res.Status == 'success') {
                            //             this.postgetList(this.page);
                            //         } else {
                            //             this.$message({
                            //                 type: 'error',
                            //                 message: '删除失败'
                            //             });
                            //         }
                            //     },
                            //     complete: (XMLHttpRequest) => {
                            //         requestPretreatment(XMLHttpRequest)
                            //     },
                            // });
                        } else {
                            this.$message({
                                type: 'error',
                                message: '删除失败'
                            });
                        }

                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                });
            }).catch(() => { });
        },
        async handleSubmit(form) {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                this.personal_mapping_tags.forEach(item => {
                    item.Personal_Tag_SerialNumber = form.Personal_Tag_SerialNumber
                })
                this.personal_tags[0].Row_Status = '1';
                let lJSONObject_DataSet = {
                    Modify:{
                        personal_tags:this.personal_tags,
                        personal_mapping_tags:this.personal_mapping_tags
                    }
                };
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                lJSONObject_DataSet['Add'] = new Object();
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSaveByRequestBody",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        this.showDialog1 = false;
                        this.postgetList(this.page);
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除失败'
                        });
                    }
                    this.loading = false;
                })
                // $.ajax({
                //     headers: requestHeaders(JSON),
                //     url: `${mh}/generalBusiness/generalSaveByRequestBody`,
                //     method: "post",
                //     data: JSON.stringify({
                //         Data: lJSONObject_DataSet,
                //     }),
                //     success: (res) => {
                //         if (res.Status == 'success') {
                //             this.showDialog1 = false;
                //             this.postgetList(this.page);
                //         } else {
                //             this.$message({
                //                 type: 'error',
                //                 message: '删除失败'
                //             });
                //         }
                //     },
                //     complete: (XMLHttpRequest) => {
                //         this.loading = false;
                //         requestPretreatment(XMLHttpRequest)
                //     },
                // });
            }
        },
        async validateAllForms() {
            //获取当前页面上所有的Elform
            let forms = [];
            for (let key in this.$refs) {
                const ref = this.$refs[key];
                if (ref.$options && ref.$options.name == 'ElForm') {
                    forms.push(ref);
                }
            }
            //判断每个Elform的校验是否通过
            let isfail = false;
            for (const form of forms) {
                try {
                    await form.validate();
                } catch (error) {
                    isfail = true;
                }
            }
            if (isfail) {
                return false;
            }
            return true;
        },
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
